package slxy.jiangtao.dts.controller.business;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import slxy.jiangtao.dts.controller.BaseController;
import slxy.jiangtao.dts.core.common.bean.Constants;
import slxy.jiangtao.dts.core.common.bean.FormCriteria;
import slxy.jiangtao.dts.core.common.bean.FormQuery;
import slxy.jiangtao.dts.core.common.bean.JSONResult;
import slxy.jiangtao.dts.core.common.hibernate.qbc.Page;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.core.util.DateUtils;
import slxy.jiangtao.dts.entity.sys.DictionariesNews;
import slxy.jiangtao.dts.entity.sys.News;
import slxy.jiangtao.dts.service.sys.NewsService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Code must be sexy
 * Created by Jiang Tao on 2017/3/20.
 * 说明：新闻收集controller
 */
@Controller
@RequestMapping("newcollect")
public class NewCollectController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(NewCollectController.class);
    @Autowired
    CommonService commonService;

    @Autowired
    NewsService newsService;

    /**
     * 新闻添加跳转页面
     */
    @RequestMapping("addPage")
    public String addPage() {
        return "news/add";
    }

    /**
     * 新闻列表页面
     */
    @RequestMapping("index")
    public String index(ModelMap modelMap) {

        FormQuery formQuery = new FormQuery("sys.News");
        ArrayList<FormCriteria> formCriterias = new ArrayList<>();
        //默认只显示本月创建的新闻
        formCriterias.add(new FormCriteria("uploadTime", "like", DateUtils.getDate("yyyy-MM") + "%"));
        Page page = commonService.getPage(formQuery);
        modelMap.addAttribute("page", JSON.toJSONString(page));
        modelMap.put("typeList", JSON.toJSONString(commonService.getList(DictionariesNews.class)));
        return "news/index";
    }

    /**
     * 新闻添加跳转页面
     */
    @RequestMapping("page/{forword}")
    public String Page(ModelMap modelMap, @PathVariable String forword, HttpServletRequest request) {
        if (forword.trim().equals("update")) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            modelMap.put("news", commonService.get(News.class, id));
        }
        modelMap.put("typeList", JSON.toJSONString(commonService.getList(DictionariesNews.class)));
        return "news/" + forword.trim().toLowerCase();
    }

    @RequestMapping("submit/{method}")
    @ResponseBody
    public JSONResult submit(@PathVariable String method, News news, HttpServletRequest request) {
        Integer id = news.getId();
        try {
            if (method.trim().equals("add") || method.trim().equals("update")) {
                id = newsService.addOrUpdate(news, getUserInSession(request));
            } else{
                return JSONResult.failure("无效访问路径");
            }
        } catch (Exception e) {
            logger.error("操作失败", e);
            return JSONResult.failure("操作失败");
        }
        return JSONResult.success(id);
    }

    @RequestMapping("delete/{newsid}")
    @ResponseBody
    public JSONResult delete(@PathVariable Integer newsid, HttpServletRequest request) {
        try {
            newsService.delete(newsid);
            logger.info(getUserInSession(request).getName() + "删除一个新闻");
        } catch (Exception e) {
            logger.error("新闻删除失败", e);
            return JSONResult.failure("新闻删除失败");
        }
        return JSONResult.success();
    }

    @RequestMapping("detail/{newsid}")
    public String detail(@PathVariable Integer newsid, ModelMap modelMap) {
        News news = commonService.get(News.class, newsid);
        commonService.evict(news);
        String context = Constants.WORD_TITLE_LEFT+news.getTitle()+Constants.WORD_TITLE_RIGHT
                +Constants.WORD_REPORTOR_ONE+news.getReporterText()+Constants.WORD_REPORTOR_TWO
                +news.getReporterPhoto()+Constants.WORD_REPORTOR_THREE
                +news.getReleaseTime()+Constants.WORD_REPORTOR_FOUR
                +news.getContent();
        news.setContent(context);
        modelMap.put("news",news );
        modelMap.put("type",commonService.get(DictionariesNews.class,news.getType()).getName());
        return "news/detail";
    }


}
